<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - coverage.lcov - src/messages/mqtt_client_mqtt_header.dart</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">src/messages</a> - mqtt_client_mqtt_header.dart<span style="font-size: 80%;"> (source / <a href="mqtt_client_mqtt_header.dart.func.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">coverage.lcov</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">49</td>
            <td class="headerCovTableEntry">51</td>
            <td class="headerCovTableEntryHi">96.1 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2017-10-09</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntry">0</td>
            <td class="headerCovTableEntryHi">-</td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : /*</a>
<span class="lineNum">       2 </span>            :  * Package : mqtt_client
<span class="lineNum">       3 </span>            :  * Author : S. Hamblett &lt;steve.hamblett@linux.com&gt;
<span class="lineNum">       4 </span>            :  * Date   : 31/05/2017
<span class="lineNum">       5 </span>            :  * Copyright :  S.Hamblett
<span class="lineNum">       6 </span>            :  */
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : part of mqtt_client;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : /// Represents the Fixed Header of an MQTT message.
<span class="lineNum">      11 </span>            : class MqttHeader {
<span class="lineNum">      12 </span>            :   /// Initializes a new instance of the MqttHeader class.
<span class="lineNum">      13 </span><span class="lineCov">          6 :   MqttHeader();</span>
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            :   /// Initializes a new instance of MqttHeader&quot; based on data contained within the supplied stream.
<span class="lineNum">      16 </span><span class="lineCov">          4 :   MqttHeader.fromByteBuffer(MqttByteBuffer headerStream) {</span>
<span class="lineNum">      17 </span><span class="lineCov">          4 :     readFrom(headerStream);</span>
<span class="lineNum">      18 </span>            :   }
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            :   /// Backing storage for the payload size.
<span class="lineNum">      21 </span>            :   int _messageSize = 0;
<span class="lineNum">      22 </span>            : 
<span class="lineNum">      23 </span>            :   /// Gets or sets the type of the MQTT message.
<span class="lineNum">      24 </span>            :   MqttMessageType messageType;
<span class="lineNum">      25 </span>            : 
<span class="lineNum">      26 </span>            :   /// Gets or sets a value indicating whether this MQTT Message is duplicate of a previous message.
<span class="lineNum">      27 </span>            :   /// True if duplicate; otherwise, false.
<span class="lineNum">      28 </span>            :   bool duplicate = false;
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            :   /// Gets or sets the Quality of Service indicator for the message.
<span class="lineNum">      31 </span>            :   MqttQos qos = MqttQos.atMostOnce;
<span class="lineNum">      32 </span>            : 
<span class="lineNum">      33 </span>            :   /// Gets or sets a value indicating whether this MQTT message should be retained by the message broker for transmission to new subscribers.
<span class="lineNum">      34 </span>            :   /// True if message should be retained by the message broker; otherwise, false.
<span class="lineNum">      35 </span>            :   /// &lt;/value&gt;
<span class="lineNum">      36 </span>            :   bool retain = false;
<span class="lineNum">      37 </span>            : 
<span class="lineNum">      38 </span>            :   ///     Gets or sets the size of the variable header + payload section of the message.
<span class="lineNum">      39 </span>            :   /// &lt;value&gt;The size of the variable header + payload.&lt;/value&gt;
<span class="lineNum">      40 </span>            :   /// &lt;exception cref=&quot;Nmqtt.InvalidPayloadSizeException&quot;&gt;The size of the variable header + payload exceeds the maximum allowed size.&lt;/exception&gt;
<span class="lineNum">      41 </span><span class="lineCov">          2 :   int get messageSize =&gt; _messageSize;</span>
<span class="lineNum">      42 </span>            : 
<span class="lineNum">      43 </span>            :   set messageSize(int value) {
<span class="lineNum">      44 </span><span class="lineCov">          2 :     if (value &lt; 0 || value &gt; Constants.maxMessageSize) {</span>
<span class="lineNum">      45 </span><span class="lineCov">          1 :       throw new InvalidPayloadSizeException(value, Constants.maxMessageSize);</span>
<span class="lineNum">      46 </span>            :     }
<span class="lineNum">      47 </span><span class="lineCov">          1 :     _messageSize = value;</span>
<span class="lineNum">      48 </span>            :   }
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            :   /// Writes the header to a supplied stream.
<span class="lineNum">      51 </span>            :   void writeTo(int messageSize, MqttByteBuffer messageStream) {
<span class="lineNum">      52 </span><span class="lineCov">          4 :     _messageSize = messageSize;</span>
<span class="lineNum">      53 </span><span class="lineCov">          4 :     final typed.Uint8Buffer headerBuff = headerBytes();</span>
<span class="lineNum">      54 </span><span class="lineCov">          4 :     messageStream.write(headerBuff);</span>
<span class="lineNum">      55 </span>            :   }
<span class="lineNum">      56 </span>            : 
<span class="lineNum">      57 </span>            :   /// Creates a new MqttHeader based on a list of bytes.
<span class="lineNum">      58 </span>            :   void readFrom(MqttByteBuffer headerStream) {
<span class="lineNum">      59 </span><span class="lineCov">          8 :     if (headerStream.length &lt; 2) {</span>
<span class="lineNum">      60 </span><span class="lineCov">          1 :       throw new InvalidHeaderException(</span>
<span class="lineNum">      61 </span>            :           &quot;The supplied header is invalid. Header must be at least 2 bytes long.&quot;);
<span class="lineNum">      62 </span>            :     }
<span class="lineNum">      63 </span><span class="lineCov">          4 :     final int firstHeaderByte = headerStream.readByte();</span>
<span class="lineNum">      64 </span>            :     // Pull out the first byte
<span class="lineNum">      65 </span><span class="lineCov">         12 :     retain = ((firstHeaderByte &amp; 1) == 1 ? true : false);</span>
<span class="lineNum">      66 </span><span class="lineCov">         16 :     qos = MqttQos.values[((firstHeaderByte &amp; 6) &gt;&gt; 1)];</span>
<span class="lineNum">      67 </span><span class="lineCov">         16 :     duplicate = (((firstHeaderByte &amp; 8) &gt;&gt; 3) == 1 ? true : false);</span>
<span class="lineNum">      68 </span><span class="lineCov">         16 :     messageType = MqttMessageType.values[((firstHeaderByte &amp; 240) &gt;&gt; 4)];</span>
<span class="lineNum">      69 </span>            : 
<span class="lineNum">      70 </span>            :     // Decode the remaining bytes as the remaining/payload size, input param is the 2nd to last byte of the header byte list
<span class="lineNum">      71 </span>            :     try {
<span class="lineNum">      72 </span><span class="lineCov">          8 :       _messageSize = readRemainingLength(headerStream);</span>
<span class="lineNum">      73 </span>            :     } catch (InvalidPayloadSizeException) {
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :       throw new InvalidHeaderException(</span>
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :           &quot;The header being processed contained an invalid size byte pattern.&quot; +</span>
<span class="lineNum">      76 </span>            :               &quot;Message size must take a most 4 bytes, and the last byte must have bit 8 set to 0.&quot;);
<span class="lineNum">      77 </span>            :     }
<span class="lineNum">      78 </span>            :   }
<span class="lineNum">      79 </span>            : 
<span class="lineNum">      80 </span>            :   /// Gets the value of the Mqtt header as a byte array
<span class="lineNum">      81 </span>            :   typed.Uint8Buffer headerBytes() {
<span class="lineNum">      82 </span><span class="lineCov">          4 :     final typed.Uint8Buffer headerBytes = new typed.Uint8Buffer();</span>
<span class="lineNum">      83 </span>            : 
<span class="lineNum">      84 </span>            :     // Build the bytes that make up the header. The first byte is a combination of message type, dup,
<span class="lineNum">      85 </span>            :     // qos and retain, and the follow bytes (up to 4 of them) are the size of the payload + variable header.
<span class="lineNum">      86 </span><span class="lineCov">         12 :     final int messageTypeLength = messageType.index &lt;&lt; 4;</span>
<span class="lineNum">      87 </span><span class="lineCov">          8 :     final int duplicateLength = (duplicate ? 1 : 0) &lt;&lt; 3;</span>
<span class="lineNum">      88 </span><span class="lineCov">         12 :     final int qosLength = qos.index &lt;&lt; 1;</span>
<span class="lineNum">      89 </span><span class="lineCov">          4 :     final int retainLength = retain ? 1 : 0;</span>
<span class="lineNum">      90 </span><span class="lineCov">          8 :     final int firstByte = messageTypeLength + duplicateLength +</span>
<span class="lineNum">      91 </span><span class="lineCov">          4 :         qosLength + retainLength;</span>
<span class="lineNum">      92 </span><span class="lineCov">          4 :     headerBytes.add(firstByte);</span>
<span class="lineNum">      93 </span><span class="lineCov">          8 :     headerBytes.addAll(getRemainingLengthBytes());</span>
<span class="lineNum">      94 </span>            :     return headerBytes;
<span class="lineNum">      95 </span>            :   }
<span class="lineNum">      96 </span>            : 
<span class="lineNum">      97 </span>            :   static int readRemainingLength(MqttByteBuffer headerStream) {
<span class="lineNum">      98 </span><span class="lineCov">          4 :     final typed.Uint8Buffer lengthBytes = readLengthBytes(headerStream);</span>
<span class="lineNum">      99 </span><span class="lineCov">          4 :     return calculateLength(lengthBytes);</span>
<span class="lineNum">     100 </span>            :   }
<span class="lineNum">     101 </span>            : 
<span class="lineNum">     102 </span>            :   /// Reads the length bytes of an MqttHeader from the supplied stream.
<span class="lineNum">     103 </span>            :   static typed.Uint8Buffer readLengthBytes(MqttByteBuffer headerStream) {
<span class="lineNum">     104 </span><span class="lineCov">          4 :     final typed.Uint8Buffer lengthBytes = new typed.Uint8Buffer();</span>
<span class="lineNum">     105 </span>            :     // Read until we've got the entire size, or the 4 byte limit is reached
<span class="lineNum">     106 </span>            :     int sizeByte;
<span class="lineNum">     107 </span>            :     int byteCount = 0;
<span class="lineNum">     108 </span>            :     do {
<span class="lineNum">     109 </span><span class="lineCov">          4 :       sizeByte = headerStream.readByte();</span>
<span class="lineNum">     110 </span><span class="lineCov">          4 :       lengthBytes.add(sizeByte);</span>
<span class="lineNum">     111 </span><span class="lineCov">         16 :     } while (++byteCount &lt;= 4 &amp;&amp; (sizeByte &amp; 0x80) == 0x80);</span>
<span class="lineNum">     112 </span>            :     return lengthBytes;
<span class="lineNum">     113 </span>            :   }
<span class="lineNum">     114 </span>            : 
<span class="lineNum">     115 </span>            :   /// Calculates and return the bytes that represent the remaining length of the message.
<span class="lineNum">     116 </span>            :   typed.Uint8Buffer getRemainingLengthBytes() {
<span class="lineNum">     117 </span><span class="lineCov">          4 :     final typed.Uint8Buffer lengthBytes = new typed.Uint8Buffer();</span>
<span class="lineNum">     118 </span><span class="lineCov">          4 :     int payloadCalc = _messageSize;</span>
<span class="lineNum">     119 </span>            : 
<span class="lineNum">     120 </span>            :     // Generate a byte array based on the message size, splitting it up into
<span class="lineNum">     121 </span>            :     // 7 bit chunks, with the 8th bit being used to indicate &quot;one more to come&quot;
<span class="lineNum">     122 </span>            :     do {
<span class="lineNum">     123 </span><span class="lineCov">          4 :       int nextByteValue = payloadCalc % 128;</span>
<span class="lineNum">     124 </span><span class="lineCov">          4 :       payloadCalc = (payloadCalc ~/ 128);</span>
<span class="lineNum">     125 </span><span class="lineCov">          4 :       if (payloadCalc &gt; 0) {</span>
<span class="lineNum">     126 </span><span class="lineCov">          1 :         nextByteValue = nextByteValue | 0x80;</span>
<span class="lineNum">     127 </span>            :       }
<span class="lineNum">     128 </span><span class="lineCov">          4 :       lengthBytes.add(nextByteValue);</span>
<span class="lineNum">     129 </span><span class="lineCov">          4 :     } while (payloadCalc &gt; 0);</span>
<span class="lineNum">     130 </span>            : 
<span class="lineNum">     131 </span>            :     return lengthBytes;
<span class="lineNum">     132 </span>            :   }
<span class="lineNum">     133 </span>            : 
<span class="lineNum">     134 </span>            :   /// Calculates the remaining length of an MqttMessage from the bytes that make up the length
<span class="lineNum">     135 </span>            :   static int calculateLength(typed.Uint8Buffer lengthBytes) {
<span class="lineNum">     136 </span>            :     var remainingLength = 0;
<span class="lineNum">     137 </span>            :     var multiplier = 1;
<span class="lineNum">     138 </span>            : 
<span class="lineNum">     139 </span><span class="lineCov">          8 :     for (int currentByte in lengthBytes) {</span>
<span class="lineNum">     140 </span><span class="lineCov">         12 :       remainingLength += (currentByte &amp; 0x7f) * multiplier;</span>
<span class="lineNum">     141 </span><span class="lineCov">          4 :       multiplier *= 0x80;</span>
<span class="lineNum">     142 </span>            :     }
<span class="lineNum">     143 </span>            :     return remainingLength;
<span class="lineNum">     144 </span>            :   }
<span class="lineNum">     145 </span>            : 
<span class="lineNum">     146 </span>            :   /// Sets the IsDuplicate flag of the header.
<span class="lineNum">     147 </span>            :   MqttHeader isDuplicate() {
<span class="lineNum">     148 </span><span class="lineCov">          1 :     this.duplicate = true;</span>
<span class="lineNum">     149 </span>            :     return this;
<span class="lineNum">     150 </span>            :   }
<span class="lineNum">     151 </span>            : 
<span class="lineNum">     152 </span>            :   /// Sets the Qos of the message header.
<span class="lineNum">     153 </span>            :   MqttHeader withQos(MqttQos qos) {
<span class="lineNum">     154 </span><span class="lineCov">          4 :     this.qos = qos;</span>
<span class="lineNum">     155 </span>            :     return this;
<span class="lineNum">     156 </span>            :   }
<span class="lineNum">     157 </span>            : 
<span class="lineNum">     158 </span>            :   /// Sets the type of the message identified in the header.
<span class="lineNum">     159 </span>            :   MqttHeader asType(MqttMessageType messageType) {
<span class="lineNum">     160 </span><span class="lineCov">          6 :     this.messageType = messageType;</span>
<span class="lineNum">     161 </span>            :     return this;
<span class="lineNum">     162 </span>            :   }
<span class="lineNum">     163 </span>            : 
<span class="lineNum">     164 </span>            :   /// Defines that the message should be retained.
<span class="lineNum">     165 </span>            :   MqttHeader shouldBeRetained() {
<span class="lineNum">     166 </span><span class="lineCov">          1 :     this.retain = true;</span>
<span class="lineNum">     167 </span>            :     return this;
<span class="lineNum">     168 </span>            :   }
<span class="lineNum">     169 </span>            : 
<span class="lineNum">     170 </span>            :   String toString() {
<span class="lineNum">     171 </span><span class="lineCov">         36 :     return &quot;Header: MessageType = $messageType, Duplicate = $duplicate, Retain = $retain, Qos = $qos, Size = $_messageSize&quot;;</span>
<span class="lineNum">     172 </span>            :   }
<span class="lineNum">     173 </span>            : }
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.10</a></td></tr>
  </table>
  <br>

</body>
</html>
